﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Web;
using System.IO;
using System.Configuration;

namespace Heckel.EasyTools.Diagramming.Drawing
{
    public class Line : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = true;
            context.Response.Clear();
            context.Response.ContentType = "image/gif";

            bool gTrans = false;
            try
            {
                gTrans = Boolean.Parse(context.Request.QueryString["transparent"].ToString());
            }
            catch { }

            bool isArrow = false;
            try
            {
                isArrow = Boolean.Parse(context.Request.QueryString["arrow"].ToString());
            }
            catch { }

            if (!gTrans)
            {
                LineDirection ld = LineDirection.Undefined;
                string dir = context.Request.QueryString["dir"].ToString();
                switch (dir)
                {
                    case "up":
                        ld = LineDirection.Up;
                        break;
                    case "down":
                        ld = LineDirection.Down;
                        break;
                }

                int getBox = Int32.Parse(context.Request.QueryString["box"].ToString());
                //Cambiado el color de las flechas
                /*Color getColor = Utils.HexToColor("#" + context.Request.QueryString["col"].ToString());*/
                Color getColor = Utils.HexToColor("#074F92");
                LineDecoration deco = StringEnum.NumToEnum<LineDecoration>
                    (Int32.Parse(context.Request.QueryString["deco"].ToString()));
                int getWidth = Int32.Parse(context.Request.QueryString["wid"].ToString());
                /*int getWidth = 2;*/

                MemoryStream mem = null;

                if (isArrow)
                {
                    //LineType lt = (LineType)StringEnum.Parse(typeof(LineType), context.Request.QueryString["lType"].ToString());
                    mem = Utils.MakeArrow(ld, getBox, getColor, deco, getWidth);
                }
                else
                    mem = Utils.MakeLine(ld, getBox, getColor, deco, getWidth);

                mem.WriteTo(context.Response.OutputStream);
            }
            else
            {
                MemoryStream mem = Utils.MakeTransparentPixel();
                mem.WriteTo(context.Response.OutputStream);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
